import bianliang from "./bianliang.js";
import BackGround from "./BackGround.js";
import Hero from "./Hero.js";
import Enemy from "./Enemy.js";
import Tool from "./Tool.js";
var game = document.querySelector("#game");
var ctx = game.getContext("2d");
bianliang.loading(function() {
	document.querySelector(".btnStart").style.display = "inline-block";
	document.querySelector(".btnStart").onclick = function() {
		startGame();
		document.querySelector(".startDiv").style.display = "none";
	}
});
var bg = null;
var hero = null; 
var timer0 = null;
var timer1 = null;

function startGame() {
	bg = new BackGround();
	hero = new Hero();
	timer0 = setInterval(function() {
		bg.draw(ctx);
		hero.draw(ctx); 
		addEnemy(); 
		addTool(); 
		isCrash();
		for (var i = 0; i < bianliang.bulletList.length; i++) {
			bianliang.bulletList[i].draw(ctx);
		}
		for (var i = 0; i < bianliang.enemyList.length; i++) {
			bianliang.enemyList[i].draw(ctx);
		}
		for (var i = 0; i < bianliang.toolList.length; i++) {
			bianliang.toolList[i].draw(ctx);
		}
		for (var i = 0; i < bianliang.boomList.length; i++) {
			bianliang.boomList[i].draw(ctx);
		}
		ctx.fillStyle = "blue";
		ctx.font = "32pt 仿宋";
		ctx.fillText("得分：" + bianliang.score, 20, 30);
	}, 50);
	timer1 = setInterval(function() {
		hero.fire();
	}, 250);
}
game.onmousemove = function(event) {
	if (hero != null) {
		var x = event.clientX - game.getBoundingClientRect().left;
		if (x > 480 - hero.width) {
			x = 480 - hero.width;
		}
		hero.x = x;
		var y = event.clientY - game.getBoundingClientRect().top;
		if (y > 850 - hero.height) {
			y = 850 - hero.height;
		}
		hero.y = y;
	}
}
function addEnemy() {
	if (bianliang.enemyList.length < 8) {
		var count = 8 - bianliang.enemyList.length;
		for (var i = 0; i < count; i++) {
			var e = new Enemy();
			bianliang.enemyList.push(e);
		}
	}
}
function addTool() {
	if (bianliang.toolList.length < 1) {
		var temp = parseInt(Math.random() * 100);
		if (temp >= 98) {
			var t = new Tool();
			bianliang.toolList.push(t);
		}
	}
}
function checkCrash(a, b) {
	if (a.x + a.width < b.x || b.x + b.width < a.x || a.y + a.height < b.y || b.y + b.height < a.y) {
		return false;
	} else {
		return true;
	}
}
function isCrash() {
	for (var i = 0; i < bianliang.toolList.length; i++) {
		var result = checkCrash(hero, bianliang.toolList[i]);
		if (result == true) {
			if (bianliang.toolList[i].type == 0) {
				hero.isTwo = true;
				bianliang.toolList.splice(i, 1);
				setTimeout(function() {
					hero.isTwo = false; 
				}, 10000);
			} else if (bianliang.toolList[i].type == 1) {
				for (var j = bianliang.enemyList.length - 1; j >= 0; j--) {
					bianliang.enemyList[j].life = 0;
					bianliang.enemyList[j].isDie();
				}
				bianliang.toolList.splice(i, 1);
			}
		}
	}
	for (var i = bianliang.bulletList.length - 1; i >= 0; i--) { 
		for (var j = bianliang.enemyList.length - 1; j >= 0; j--) {
			var result = checkCrash(bianliang.bulletList[i], bianliang.enemyList[j]);
			if (result) {
				bianliang.bulletList.splice(i, 1);
				bianliang.enemyList[j].life--;
				bianliang.enemyList[j].isDie();
				break; 
			}
		}
	}
	for (var i = bianliang.enemyList.length - 1; i >= 0; i--) {
		var result = checkCrash(bianliang.enemyList[i], hero);
		if (result) {
			clearInterval(timer0);
			clearInterval(timer1);
			document.querySelector(".center").innerText=bianliang.score;
			document.querySelector(".endDiv").style.display="block";
		}
	}
}
